package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Review;
import cn.wolfcode.trip.base.mapper.ReviewMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.ReviewQueryObject;
import cn.wolfcode.trip.base.service.IReviewService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
public class ReviewServiceImpl implements IReviewService {
    @Autowired
    private ReviewMapper reviewMapper;

    @Override
    public PageInfo<Review> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage() , qo.getPageSize() , "r.createTime desc");
        List<Review> list = reviewMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    @Override
    public Review saveOrUpdate(Review entity) {
        if (entity.getId() != null){
            reviewMapper.updateByPrimaryKey(entity);
        }else {
            entity.setCreateTime(new Date());
            entity.setReaded(0);
            reviewMapper.insert(entity);
        }
        return entity;
    }

    @Override
    public List<Review> getTravel(ReviewQueryObject qo) {
        return reviewMapper.listTraverl(qo);
    }

    @Override
    public List<Review> getComment(ReviewQueryObject qo) {
        return  reviewMapper.listComment(qo);
    }

    @Override
    public List<Review> getReview(ReviewQueryObject qo) {
        return reviewMapper.getReview(qo);
    }
    //修改状态
    @Override
    public void update(ReviewQueryObject qo) {
        reviewMapper.update(qo);
    }
    //查询at当前用户的信息
    @Override
    public List getAtUser(String str) {
        List<Review> atUser = reviewMapper.getAtUser(str);
        for (Review review : atUser) {
            String[] split = review.getReview().split(str);
            String st ="";
            for (String s : split){
                if(!",".equals(s)){
                    st=s+st;
                }
            }

            review.setReview(st);
        }
        return  atUser;
    }
    //at我未读数量
    @Override
    public List getAt(String str) {
        List<Review> atUser = reviewMapper.getAt(str);
        return atUser;
    }

    @Override
    public void updateAtreaded(String str) {
        reviewMapper.updateAtreaded(str);
    }
}
